<?php

/**
 * 评论展示
 */

namespace app\mall\common;

class CommentCommon extends \app\base\common\SiteCommon {

    protected $id = 0;
    protected $info = [];
    protected $classInfo = [];
    protected $crumb = [];

    public function __construct() {
        parent::__construct();

        $id = request('get', 'id', 0, 'intval');
        if(empty($id)){
            $this->error404();
        }

        $info = target('mall/Mall')->getInfo($id);
        if(empty($info) || !$info['status']) {
            $this->error404();
        }

        $classInfo = target('mall/MallClass')->getInfo($info['class_id']);
        if(empty($classInfo)) {
            $this->error404();
        }

        $info = target('mall/Mall')->getInfo($id, $classInfo['model_id']);

        $this->setMeta('评论 - ' . $info['title']);

        $crumb = target('mall/MallClass')->loadCrumbList($info['class_id']);
        $this->setCrumb($crumb);

        $info['images'] = unserialize($info['images']);

        $this->id = $id;
        $this->info = $info;
        $this->classInfo = $classInfo;
        $this->crumb = $crumb;

    }

    protected function index() {

        $id = $this->id;
        $info = $this->info;
        $classInfo = $this->classInfo;
        $crumb = $this->crumb;

        $this->pageInfo['name'] = '商品评论';
        $type = request('get', 'type', 0, 'intval');

        $urlParam = [];
        $urlParam['id'] = $id;
        $urlParam['type'] = $type;

        $where = [];
        switch($type) {
            case 1:
                $where['level'] = 2;
                break;
            case 2:
                $where['level'] = 1;
                break;
            case 3:
                $where['level'] = 0;
                break;
        }
        $where['app'] = 'mall';
        $where['has_id'] = $id;
        $pageLimit = 20;

        $model = target('order/OrderComment');
        $count = $model->countList($where);
        $pageData = $this->pageData($count, $pageLimit, $urlParam);
        $list = $model->loadList($where, $pageData['limit'], 'time desc');

        $countCommentPositive = target('order/OrderComment')->countList([
            'app' => 'mall',
            'has_id' => $id,
            'level' => 2
        ]);
        $countCommentNeutral = target('order/OrderComment')->countList([
            'app' => 'mall',
            'has_id' => $id,
            'level' => 1
        ]);
        $countCommentNegative = target('order/OrderComment')->countList([
            'app' => 'mall',
            'has_id' => $id,
            'level' => 0
        ]);

        $sumComment = $countCommentPositive + $countCommentNeutral + $countCommentNegative;
        $commentPositiveRate = $sumComment ? round(($countCommentPositive / $sumComment) * 100) : 0;
        $commentNeutralRate = $sumComment ? round(($countCommentNeutral / $sumComment) * 100) : 0;
        $commentNegativeRate = $sumComment ? round(($countCommentNegative / $sumComment) * 100) : 0;

        $this->assign('pageList', $list);
        $this->assign('page', $pageData['html']);
        $this->assign('info', $info);
        $this->assign('classInfo', $classInfo);

        $this->assign('commentRate', [
            'positive' => $commentPositiveRate,
            'neutral' => $commentNeutralRate,
            'negative' => $commentNegativeRate,
        ]);
        $this->assign('commentCount', [
            'positive' => $countCommentPositive,
            'neutral' => $countCommentNeutral,
            'negative' => $countCommentNegative,
        ]);

        $this->siteDisplay('mall_comment');
    }


}